using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Purchasing;
using UnityEngine.Purchasing.Security;

namespace Voodoo.Sauce.Internal.IAP
{
	internal class StoreListener /*: IStoreListener*/
	{
		private const string TAG = "StoreListener";

		//private IStoreController _storeController;

		//private IExtensionProvider _storeExtensionProvider;

		private bool _isInitialized;

		private readonly List<string> _pendingPurchases;

		private bool _isRestoringPurchases;

		public StoreListener()
		{
			_pendingPurchases = new List<string>();
		}

		internal void Init(ProductDescriptor[] products)
		{
			//ConfigurationBuilder configurationBuilder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
			//foreach (ProductDescriptor productDescriptor in products)
			//{
			//	configurationBuilder.AddProduct(productDescriptor.ProductId, productDescriptor.Type);
			//}
			//UnityPurchasing.Initialize(this, configurationBuilder);
		}

		//public void OnInitialized(IStoreController controller, IExtensionProvider extensions)
		//{
		//	VoodooLog.Log("StoreListener", "Purchase manager initialized succesfully.");
		//	_storeController = controller;
		//	_storeExtensionProvider = extensions;
		//	_isInitialized = true;
		//	if (_pendingPurchases.Count > 0)
		//	{
		//		VoodooLog.Log("StoreListener", "Processing " + _pendingPurchases.Count + " pending purchase requests ...");
		//		foreach (string pendingPurchase in _pendingPurchases)
		//		{
		//			BuyProduct(pendingPurchase);
		//		}
		//	}
		//	_pendingPurchases.Clear();
		//	VoodooIAP.OnInitializeSuccess();
		//}

		//public void OnInitializeFailed(InitializationFailureReason reason)
		//{
		//	VoodooLog.Log("StoreListener", "Purchase manager failed to initialize : " + reason);
		//	VoodooIAP.OnInitializeFailed(reason);
		//}

		internal void BuyProduct(string productId)
		{
			if (!_isInitialized)
			{
				VoodooLog.Log("StoreListener", "Purchase manager is not yet initialized, enqueuing purchase ...");
				_pendingPurchases.Add(productId);
				return;
			}
			VoodooLog.Log("StoreListener", "Purchasing product with ID " + productId + ". Searching for product ...");
			//Product product = _storeController.products.WithID(productId);
			//if (product == null)
			//{
			//	throw new Exception("No product with ID " + productId);
			//}
			//if (!product.availableToPurchase)
			//{
			//	VoodooLog.Log("StoreListener", "The product is not available!");
			//	OnPurchaseFailed(product, PurchaseFailureReason.ProductUnavailable);
			//}
			//else
			//{
			//	VoodooLog.Log("StoreListener", "Product found, purchasing ...");
			//	_storeController.InitiatePurchase(product);
			//}
		}

		//public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs purchase)
		//{
		//	bool flag = true;
		//	CrossPlatformValidator crossPlatformValidator = new CrossPlatformValidator(GooglePlayTangle.Data(), AppleTangle.Data(), Application.identifier);
		//	try
		//	{
		//		crossPlatformValidator.Validate(purchase.purchasedProduct.receipt);
		//	}
		//	catch (IAPSecurityException ex)
		//	{
		//		VoodooLog.Log("StoreListener", "Invalid receipt, not unlocking content, msg: '" + ex.Message + "'");
		//		flag = false;
		//	}
		//	if (flag)
		//	{
		//		VoodooLog.Log("StoreListener", "Purchase complete for product with ID " + purchase.purchasedProduct.definition.id);
		//		VoodooIAP.ProcessPurchase(purchase.purchasedProduct, _isRestoringPurchases);
		//	}
		//	else
		//	{
		//		VoodooLog.Log("StoreListener", "Invalid purchase signature, aborting.");
		//		VoodooIAP.OnPurchaseFailed(purchase.purchasedProduct.definition.id, PurchaseFailureReason.SignatureInvalid);
		//	}
		//	return PurchaseProcessingResult.Complete;
		//}

		//public void OnPurchaseFailed(Product product, PurchaseFailureReason reason)
		//{
		//	VoodooLog.Log("StoreListener", "Purchasing product with ID " + product.definition.id + " failed : " + reason);
		//	VoodooIAP.OnPurchaseFailed(product.definition.id, reason);
		//}

		internal void RestorePurchases()
		{
			if (Application.platform != RuntimePlatform.IPhonePlayer)
			{
				VoodooLog.Log("StoreListener", "Restore purchases is not supported on this platform.");
				return;
			}
			//if (!_isInitialized || _storeExtensionProvider == null)
			//{
			//	VoodooLog.Log("StoreListener", "Store has not been initialized yet. Skipping restore purchases.");
			//	return;
			//}
			//IAppleExtensions extension = _storeExtensionProvider.GetExtension<IAppleExtensions>();
			_isRestoringPurchases = true;
			//extension.RestoreTransactions(delegate(bool result)
			//{
			//	if (!result)
			//	{
			//		VoodooLog.Log("StoreListener", "Restoration process failed.");
			//	}
			//	else
			//	{
			//		VoodooLog.Log("StoreListener", "Restoration process succeeded. It may succeed multiple times, don't worry.");
			//	}
			//	_isRestoringPurchases = false;
			//});
		}

		//public string GetLocalizedProductPrice(string productId)
		//{
		//	//if (_storeController == null)
		//	//{
		//	//	VoodooLog.LogW("StoreListener", "Store Controller has not been initialized. Don't call this method before the IPurchaseDelegate.OnInitializeSuccess callback.");
		//	//	return null;
		//	//}
		//	//return _storeController.products.WithID(productId)?.metadata.localizedPriceString;
		//}
	}
}
